﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManage
{
    public partial class Admin2 : Form
    {
        public Admin2()
        {
            InitializeComponent();
            Table();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Admin22 admin22 = new Admin22();   
            admin22.ShowDialog();  
            Table();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Admin2_Load(object sender, EventArgs e)
        {
            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() 
                + " " + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }
        private void Table()
        {
            dataGridView1.Rows.Clear();
            Dao dao = new Dao();
            string sql = "select * from t_book;";
            IDataReader dc = dao.read(sql);

            while(dc.Read())
            {
                dataGridView1.Rows.Add(dc["id"].ToString(), dc["name"].ToString(), dc["author"].ToString(), dc["press"].ToString(),dc["number"].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

           
            try
            {
                DialogResult dr = MessageBox.Show("你确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
             
                if (dr == DialogResult.OK)
                {
                    
                    string sql = $"delete from t_book where id = '{id}';";

                    Dao dao = new Dao();
                    if(dao.Execute(sql)>0)
                    {
                        Table();
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                   
                    
                }
                
            }
            catch
            {
                MessageBox.Show("未选中行");
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + " " + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string id = dataGridView1.SelectedRows[0].Cells[0].    Value.ToString();
            string name = dataGridView1.SelectedRows[0].Cells[1].  Value.ToString();
            string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            string press = dataGridView1.SelectedRows[0].Cells[3]. Value.ToString();
            string number = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            Admin23 admin23 = new Admin23(id,name,author,press,number);
            admin23.ShowDialog();
            Table();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Table();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            string sql = $"select * from t_book where id = '{textBox1.Text}';";
            Dao dao = new Dao();   
            IDataReader dc  = dao.read(sql);
            while(dc.Read())
            {
                dataGridView1.Rows.Add(dc["id"].ToString(), dc["name"].ToString(), dc["author"].ToString(), dc["press"].ToString(), dc["number"].ToString());
            }
            dc.Close();
            dao.DaoClose();
            textBox1.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            string sql = $"select * from t_book where name like '%{textBox2.Text}%';";
            Dao dao = new Dao();
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc["id"].ToString(), dc["name"].ToString(), dc["author"].ToString(), dc["press"].ToString(), dc["number"].ToString());
            }
            dc.Close();
            dao.DaoClose();
            textBox2.Text = "";
        }
    }
}
